﻿@{
    Layout = "_LayoutH";
    @model Sys_dept

}
<div id="app" v-cloak>
    <form class="form-horizontal" onsubmit="return false">
        <div class="box-body">
            <div class="form-group">
                <label class="col-sm-2 control-label">名称</label>
                <div class="col-sm-10">
                    <input class="form-control" v-model="DeptName" type="text">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label">父级名称</label>
                <div class="col-sm-10">
                    <select v-model="ParentId" class="form-control" type="text">
                        <option v-for="item in DeptNameList" v-bind:value="item.DeptId" v-text="item.DeptName" v-bind:selected="item.DeptId==ParentId"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">编号</label>
                <div class="col-sm-10">
                    <input class="form-control" v-model="DeptNo" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">下级权限</label>
                <div class="col-sm-10">
                    <label><input type="radio" name="manage" v-model="IsManage" value="1" />允许</label>
                    <label style="margin-left:10px;"><input type="radio" name="manage" v-model="IsManage" value="0" />禁止</label>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">备注</label>
                <div class="col-sm-10">
                    <textarea v-model="Remark" class="form-control" rows="3" placeholder="备注...100个字符以内"></textarea>
                    <p class="textarea-numberbar"><em class="textarea-length" v-cloak>{{count}}</em>/100</p>
                </div>
            </div>
        </div>
        <div class="box-footer">
            <div class="pull-right box-tools">
                @*<button class="btn btn-primary" type="button">保存</button>*@
                <input v-on:click="addL" class="btn btn-primary radius" type="submit" v-model="submit">
            </div>
        </div>
    </form>
</div>
@section scripts{
    <script>
        var app = new Vue({
            el: "#app",
            data: {
                submit: "添加",
                DeptId:"@Model.DeptId",
                Remark: "@Model.Remark",
                ParentId: "@Model.ParentId",
                DeptName:'@Model.DeptName',
                DeptNo: "@Model.DeptNo",
                IsManage: "@Model.IsManage",
                //DeptNoList: ['省级', '市级', '公司级'],
                DeptNoList: [],
                DeptParentResult: @Html.Raw(ViewBag.parentName),
                DeptNameList: [],
                DeptAdmin:'管理员级'
            },
            computed: {
                count: function () {
                    return this.strLength(this.Remark, false);
                }
            },
            mounted: function(){
                var _self = this;
                _self.$nextTick(function () {
                    if (_self.DeptId > 0) {

                        _self.submit = "修改";
                    }
                    if (_self.DeptParentResult.length>0) {
                        var res = eval("(" + _self.DeptParentResult + ")");
                        res.forEach((item) => {
                            if (item.IsManage == 1) {
                                _self.DeptNameList.push({ DeptId: item.DeptId, DeptNo: item.DeptNo, DeptName: item.DeptName });
                            }
                        });
                    }
                    //_self.$refs.Remark.clientWidth = _self.$refs.DeptName.clientWidth;
                });
            },
            methods: {
                addL: function () {
                    var _self = this;
                    if (_self.DeptNo.length <= 0) {
                        layer.msg("编号不能为空", { icon: 2 });
                        return false;
                    }
                    if (_self.DeptName.length <= 0) {
                        layer.msg("名称不能为空", { icon: 2 });
                        return false;
                    }
                    if (_self.ParentId <= 0) {
                        layer.msg("父级名称不能为空", { icon: 2 });
                        return false;
                    }
                    var index = layer.load(1, {
                        shade: [0.1, '#fff'] //0.1透明度的白色背景
                    });

                    var data = {
                        id: _self.DeptId,
                        DeptName: _self.DeptName,
                        DeptNo: _self.DeptNo,
                        ParentId: _self.ParentId,
                        Remark: _self.Remark,
                        IsManage: _self.IsManage
                    };
                    yui.$axiospostform('/Dept/AddOrUpdate', data)
                        .then(function (response) {
                            if (response.data.Item1 === 101) {
                                layer.tips(response.data.Item2, '..layui-layer-setwin', {
                                    tips: [1, '#3595CC'],
                                    time: 3000
                                });
                                layer.close(index);
                                parent.app.searchL();
                                return false;
                            }
                            if (response.data.Item1) {
                                layer.msg(response.data.Item2, { icon: 1, time: 1000 });
                                setTimeout(function () {
                                    yui.layer_close3();
                                }, 500);
                            } else {
                                layer.msg(response.data.Item2 || errorMsg, { icon: 5 });
                            }
                            layer.close(index);
                            parent.app.searchL();
                        })
                        .catch(function (error) {
                            layer.close(index);
                        });
                },
            },
            watch: {
                //DeptNo() {
                //    var _self = this;
                //    var data = {
                //        DeptNo: _self.DeptNo
                //    };
                //    var index = layer.load(1, {
                //        shade: [0.1, '#fff'] //0.1透明度的白色背景
                //    });
                //    yui.$axiospostform('/Dept/GetFatherContent', data)
                //        .then(function (response) {
                //            _self.DeptNameList = [];
                //            var result = response.data;
                //            result.forEach((item) => {
                //                if (item.IsManage == 1) {
                //                    _self.DeptNameList.push({ DeptId: item.DeptId, DeptNo: item.DeptNo, DeptName: item.DeptName });
                //                }
                //            });
                //            layer.close(index);
                //        })
                //        .catch(function (error) {
                //            layer.close(index);
                //        });
                //}
            }
        });
    </script>
}
